package com.exhibition.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.exhibition.service.ITbVenueService;
import com.exhibition.util.Util;

@Controller
public class TbVenueController {
	
	@Autowired
	ITbVenueService tbVenueService;
	
	@RequestMapping("admin/tbvenueList.do")	
	public ModelAndView list(HttpServletRequest request,String ven_name){
		ModelAndView mav=new ModelAndView("./tbvenue/list.jsp");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("ven_name", ven_name);
		mav.addObject("list", tbVenueService.getList(map));
		mav.addObject("ven_name", ven_name);
		return mav;
	}
	@RequestMapping("admin/tbvenueAddOk.do")	
	public ModelAndView addOk(HttpServletRequest request,
			  String ven_name,
			  String ven_no,
			  String ven_address,
			  String ven_host,
			  String ven_phone,
			  String ven_area,
			  String ven_exh_no,
			  String ven_height,
			  String ven_weight,
			  String ven_door_height,
			  String ven_door_width,
			  String ven_remark,
			  @RequestParam(value = "file", required = false) MultipartFile file){
				//上传文件
				String mate_url="";
				String file_name="";
				File fi=new File(request.getSession().getServletContext().getRealPath("/") + "upload/");
				if(!fi.exists()){
					fi.mkdirs();
				}
				String[] fString=file.getOriginalFilename().split("\\.");
				String fn=Util.getPrimaryKey()+"."+fString[fString.length-1];
				String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"  
		                + fn;
				File f=new File(filePath);
				try {
					file.transferTo(f);
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				mate_url="upload/"+fn;
				ModelAndView mav=new ModelAndView("./tbvenue/add.jsp");
				Map<String,Object> map=new HashMap<String,Object>();
				  map.put("ven_id",Util.getPrimaryKey());
				  map.put("ven_name",ven_name);
				  map.put("ven_no",ven_no);
				  map.put("ven_address",ven_address);
				  map.put("ven_host",ven_host);
				  map.put("ven_phone",ven_phone);
				  map.put("ven_pic_url",mate_url);
				  map.put("ven_area",ven_area);
				  map.put("ven_exh_no",ven_exh_no);
				  map.put("ven_height",ven_height);
				  map.put("ven_weight",ven_weight);
				  map.put("ven_door_height",ven_door_height);
				  map.put("ven_door_width",ven_door_width);
				  map.put("ven_remark", ven_remark);
				  tbVenueService.add(map);
		return mav;
	}
	@RequestMapping("admin/tbvenueShowPic.do")	
	public ModelAndView ShowPic(HttpServletRequest request,String img){
		ModelAndView mav=new ModelAndView("./tbvenue/showpic.jsp");
		mav.addObject("img", img);
		return mav;
	}
	@RequestMapping("admin/tbvenueUpdate.do")	
	public ModelAndView update(HttpServletRequest request,String ven_id){
		ModelAndView mav=new ModelAndView("./tbvenue/update.jsp");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("ven_id", ven_id);
		mav.addObject("tbvenue", tbVenueService.getOne(map));
		return mav;
	}
	@RequestMapping("admin/tbvenueUpdateOk.do")	
	public ModelAndView updateOk(HttpServletRequest request,
			      String ven_id,
			      String ven_name,
				  String ven_no,
				  String ven_address,
				  String ven_host,
				  String ven_phone,
				  String ven_area,
				  String ven_exh_no,
				  String ven_height,
				  String ven_weight,
				  String ven_door_height,
				  String ven_door_width,
				  String ven_remark){
		ModelAndView mav=new ModelAndView("redirect:/admin/tbvenueList.do");
		Map<String,Object> map=new HashMap<String,Object>();
		  map.put("ven_id",ven_id);
		  map.put("ven_name",ven_name);
		  map.put("ven_no",ven_no);
		  map.put("ven_address",ven_address);
		  map.put("ven_host",ven_host);
		  map.put("ven_phone",ven_phone);
		  map.put("ven_area",ven_area);
		  map.put("ven_exh_no",ven_exh_no);
		  map.put("ven_height",ven_height);
		  map.put("ven_weight",ven_weight);
		  map.put("ven_door_height",ven_door_height);
		  map.put("ven_door_width",ven_door_width);
		  map.put("ven_remark", ven_remark);
		  tbVenueService.update(map);
		return mav;
	}
	@RequestMapping("admin/tbvenueDelete.do")	
	public ModelAndView delete(HttpServletRequest request,String ven_id){
		ModelAndView mav=new ModelAndView("redirect:/admin/tbvenueList.do");
		tbVenueService.delete(ven_id);
		return mav;
	}
}
